#    Copyright 2010 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import COOPGIS_Ajax, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import COOPGIS_FormMisc, COOPGIS_FormInventoryCollection, COOPGIS_FormGeography, COOPGIS_FormAssembly
import COOPGIS_FormSubstation, COOPGIS_FormGeneral, COOPGIS_FormMisc
import COOPGIS_DynamicTemplate
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms

def GeneralMainMenu(request):
    ApplicationName=djangoproject.settings.COOPGISGeneralApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName) 
    t = loader.get_template('/opt/coopgis/djangoproject/templates/generalmainmenu.html')
    c = Context({'LoginStatus':LoginStatus})
    return HttpResponse(t.render(c))

def CompanySetup(request):
    ApplicationName=djangoproject.settings.COOPGISGeneralApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/companysetup.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'CompanySetup')

    company=COOPGIS_FormGeneral.ProcessCompanySetupForm(form, UserName=UserNameOrIPAddress)

    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'CompanySetup', SpecialBuildFormInfo=company)

    form=COOPGIS_FormGeneral.PopulateCompanySetupForm(form, company)

    t = loader.get_template('/opt/coopgis/djangoproject/templates/companysetup.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

def VoltageSetup(request):
    ApplicationName=djangoproject.settings.COOPGISGeneralApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/voltagesetup.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'VoltageSetup')
    voltage=COOPGIS_FormGeneral.ProcessVoltageSetupForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'VoltageSetup', SpecialBuildFormInfo=voltage)
    form=COOPGIS_FormGeneral.PopulateVoltageSetupForm(form, voltage)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/voltagesetup.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

def CategorizeCompatibleUnit(request):
    ApplicationName=djangoproject.settings.COOPGISGeneralApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/mainmenu.html')
        c = Context({'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'CategorizeByCompatibleUnit')
    CompatibleUnitChoice, EditCount=COOPGIS_FormGeneral.ProcessCompatibleUnitCategorizationForm(form, UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'CategorizeByCompatibleUnit')
    form=COOPGIS_FormGeneral.PopulateCompatibleUnitCategorizationForm(form, CompatibleUnitChoice, EditCount)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/general_compatibleunitcategorization.html')
    c = Context({'form': form, 'LoginStatus':LoginStatus})
    return HttpResponse(t.render(c))

